package com.daykw.douyin.bak;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.daykw.douyin.config.NutzDaoConfig;
import com.daykw.douyin.http.HttpBase;
import com.daykw.douyin.model.VideoAuth;
import com.daykw.douyin.model.VideoInfo;
import com.daykw.douyin.model.VideoTask;
import com.daykw.douyin.model.VideoUserSearch;
import com.daykw.douyin.utils.SpringContextHolder;

public class VideoUserVideoThread extends Thread {

	private static Logger logger = Logger.getLogger(VideoUserVideoThread.class);
	private NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);

	@Override
	public void run() {
		try {
			List<VideoUserSearch> list = dao.query(VideoUserSearch.class,
					Cnd.where("video_status", "=", 0).limit(1, 10));

			HttpBase http = new HttpBase();
			Map<String, String> headers = new HashMap<String, String>();

			for (VideoUserSearch v : list) {
				List<Record> cookies = dao.query("t_cookie_search", Cnd.wrap("WHERE id=14 ORDER BY RAND() limit 1"));
				Record cookie = cookies.get(0);
				String c = cookie.getString("cookie");
				headers.put("Cookie", c);
				VideoAuth auth = dao.fetch(VideoAuth.class, Cnd.where("dataid", "=", v.getDataid()));

				String surl = "https://dy.feigua.cn/Blogger/Awemes";
				if (StringUtils.isNotEmpty(auth.getMcnid())) {
					Map<String, String> data = new HashMap<String, String>();
					data.put("uid", auth.getKw_uid());
					data.put("id", v.getDataid());
					data.put("mcnid", auth.getMcnid());

					String html = http.post(surl, headers, data);
					if (!html.equals("fail")) {
						Document doc = Jsoup.parse(html);

						Elements trs = doc.select("table.table tbody tr");

						for (Element tr : trs) {

							Element a = tr.select("div.media-list div.item-media a").first();
							String dataid = a.attr("data-id");
							String awemeid = a.attr("data-awemeid");
							String img = tr.select("div.media-list div.item-media a img").attr("src");
							String title = tr.select("div.media-list div.item-title a").text();

							String authurl = auth.getAuth_url();
							String infourl = "https://dy.feigua.cn/Aweme/Detail?id=" + dataid + "&awemeId=" + awemeid
									+ "&active=detail";

							this.saveData(dataid, awemeid, infourl, authurl, img, auth.getKw_uid());

						}
						dao.update(VideoUserSearch.class, Chain.make("video_status", 2),
								Cnd.where("dataid", "=", v.getDataid()));
						logger.info(v.getName() + "账号下的视频搜索完毕");
					}
				} else {
					logger.info("mcndid 为空" + v.getName());
					dao.update(VideoUserSearch.class, Chain.make("video_status", 3),
							Cnd.where("dataid", "=", v.getDataid()));
				}

				Thread.sleep(2000);
			}
		} catch (Exception e) {
			logger.error("", e);
		}

	}

	private void saveData(String dataid, String awemeid, String url, String auth_url, String img, String uid) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
		VideoTask task = dao.fetch(VideoTask.class, Cnd.where("kw_id", "=", awemeid));

		if (task == null) {
			task = new VideoTask();

			task.setInfo_url(url);
			task.setDataid(dataid);
			task.setKw_id(awemeid);

			task.setAuth_url(auth_url);

			task.setInfo_status(9);
			task.setUid_status(9);
			task.setCmt_status(9);
			task.setEc_status(5);
			task.setCreate_time(new Date());

			task.setCdate(Integer.parseInt(fmt.format(new Date())));
			try {
				dao.fastInsert(task);
			} catch (Exception e) {
				logger.error("", e);
			}

			VideoInfo video = new VideoInfo();

			video.setHas_product(0);

			video.setKw_head_image(img);
			video.setDataid(dataid);
			video.setKw_id(awemeid);
			video.setKw_uid(uid);
			video.setCreate_time(new Date());
			video.setCdate(Integer.parseInt(fmt.format(new Date())));
			try {
				dao.fastInsert(video);
			} catch (Exception e) {
				logger.error("", e);
			}

		} else {
			logger.info("awemeid重复：" + awemeid);
		}
	}
}
